﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;

namespace OrmAuto
{

		/// <summary>
		/// Descrizione di riepilogo per ABaseConn
		/// </summary>
		public abstract class ABaseEF
		{


				//private EntityConnection EntityCONN { get; set; }

				//private DBEntities DAL { get; set; }

				//public ObjectContext DALbase { get; set; }


				public EntityConnection getEntityConn(string pNameConnString)
				{
						if (System.Configuration.ConfigurationManager.ConnectionStrings[pNameConnString] == null)
								throw new ArgumentException("La stringa di connessione " + pNameConnString + " non esiste.");
						return new EntityConnection("name=" + pNameConnString);
				}

				public EntityCommand getEntityCommand(string commandText, CommandType ctype, EntityConnection eConn)
				{
						if (!tryToOpenConn(eConn)) return null;
						var cmd = eConn.CreateCommand();
						cmd.CommandText = commandText;//"AdventureWorksEntities.GetOrderDetails"
						cmd.CommandType = ctype;
						//EntityParameter param = new EntityParameter();
						//param.Value = "43659";
						//param.ParameterName = "SalesOrderHeaderId";
						//cmd.Parameters.Add(param);

						return cmd;
				} //fM



				public bool tryToOpenConn(EntityConnection eConn)
				{
						if (eConn == null) return false;
						try
						{
								eConn.Open();
								return true;
						}
						catch (Exception ex)
						{
								return false;
						}
				}//fM

				public bool tryToOpenConn(IDbConnection Conn)
				{
						if (Conn == null) return false;
						if(Conn.State == ConnectionState.Open)return true ;
						try
						{
								Conn.Open();
								return true;
						}
						catch (Exception ex)
						{
								return false;
						}
				}//fM


				public DBEntities getDAL()
				{
						return new DBEntities();
				}//fM


				public ObjectContext getDALBase()
				{
						return getDAL() as ObjectContext;
				}//fM

		}//fC
}//fN